package com.cash.app.utils.extra.tools

import android.annotation.SuppressLint
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.provider.Settings
import android.text.TextUtils
import java.util.Locale

object SkipPermissionUtils {
    fun gotoPermission(context: Context) {
        val brand = Build.BRAND
        if (TextUtils.equals(brand.lowercase(Locale.getDefault()), "redmi") || TextUtils.equals(
                brand.lowercase(
                    Locale.getDefault()
                ), "xiaomi"
            )
        ) {
            gotoMiuiPermission(context) //小米
        } else if (TextUtils.equals(brand.lowercase(Locale.getDefault()), "meizu")) {
            gotoMeizuPermission(context)
        } else if (TextUtils.equals(
                brand.lowercase(Locale.getDefault()), "huawei"
            ) || TextUtils.equals(
                brand.lowercase(
                    Locale.getDefault()
                ), "honor"
            )
        ) {
            gotoHuaweiPermission(context)
        } else {
            context.startActivity(getAppDetailSettingIntent(context))
        }
    }

    private fun gotoMiuiPermission(context: Context) {
        val miVersion = readMIVersion()
        try {
            if (miVersion == "V5") {
                val localIntent = Intent(
                    Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
                    Uri.parse("package:" + context.packageName)
                )
                context.startActivity(localIntent)
            } else if (miVersion == "V6" || miVersion == "V7") {
                val localIntent = Intent("miui.intent.action.APP_PERM_EDITOR")
                localIntent.setClassName(
                    "com.miui.securitycenter",
                    "com.miui.permcenter.permissions.AppPermissionsEditorActivity"
                )
                localIntent.putExtra("extra_pkgname", context.packageName)
                context.startActivity(localIntent)
            } else if (miVersion == "V9" || miVersion == "V8") {
                val localIntent = Intent("miui.intent.action.APP_PERM_EDITOR")
                localIntent.setClassName(
                    "com.miui.securitycenter",
                    "com.miui.permcenter.permissions.PermissionsEditorActivity"
                )
                localIntent.putExtra("extra_pkgname", context.packageName)
                context.startActivity(localIntent)
            } else {
                context.startActivity(getAppDetailSettingIntent(context))
            }
        } catch (e: Exception) {
            context.startActivity(getAppDetailSettingIntent(context))
        }
    }

    private fun gotoMeizuPermission(context: Context) {
        try {
            context.startActivity(getAppDetailSettingIntent(context))
        } catch (e: Exception) {
            e.printStackTrace()
            val intent = Intent("com.meizu.safe.security.SHOW_APPSEC")
            intent.addCategory(Intent.CATEGORY_DEFAULT)
            intent.putExtra("packageName", context.packageName)
            context.startActivity(intent)
        }
    }

    private fun gotoHuaweiPermission(context: Context) {
        try {
            context.startActivity(getAppDetailSettingIntent(context))
        } catch (e: Exception) {
            e.printStackTrace()
            val intent = Intent()
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            val comp = ComponentName(
                "com.huawei.systemmanager", "com.huawei.permissionmanager.ui.MainActivity"
            ) //华为权限管理
            intent.setComponent(comp)
            context.startActivity(intent)
        }
    }

    private fun getAppDetailSettingIntent(context: Context): Intent {
        val localIntent = Intent()
        localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        if (Build.VERSION.SDK_INT >= 9) {
            localIntent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS")
            localIntent.setData(Uri.fromParts("package", context.packageName, null))
        } else if (Build.VERSION.SDK_INT <= 8) {
            localIntent.setAction(Intent.ACTION_VIEW)
            localIntent.setClassName(
                "com.android.settings", "com.android.settings.InstalledAppDetails"
            )
            localIntent.putExtra("com.android.settings.ApplicationPkgName", context.packageName)
        }
        return localIntent
    }


    fun readMIVersion(): String? {
        var versionName = ""
        try {
            @SuppressLint("PrivateApi") val propertyClass =
                Class.forName("android.os.SystemProperties")
            val method = propertyClass.getMethod("get", String::class.java)
            versionName = method.invoke(propertyClass, "ro.miui.ui.version.name") as String
            return versionName
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return versionName
    }
}
